/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ideal.Interface;

import com.ideal.log.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author Huyu
 */
public class HttpSend {
    private String urlStr = "http://192.168.1.104/idea80/data/bcm";//默认ideal云服务地址
    private final String KeyName;//http协议body中的key名称
    public HttpSend(String url){
        urlStr = url;
        KeyName = "data";
    }
    
    public void sendMessage(String info) throws MalformedURLException, IOException{
        URL url = new URL(urlStr);
        URLConnection URLconnection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
        httpConnection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(httpConnection.getOutputStream(), "utf-8");
        out.write(KeyName + "=" + info); // 向页面传递数据。post的关键所在！  
        //Log.logger.info("send message is "+KeyName + "=" + info);
        out.flush();  
        out.close();  
      
        InputStream urlStream = httpConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));
        String sCurrentLine = "";
        String sTotalString = "";
        while ((sCurrentLine = bufferedReader.readLine()) != null) {
            sTotalString += sCurrentLine;
            Log.logger.info(sCurrentLine);
        }
        //假设该url页面输出为"OK" 
        httpConnection.disconnect();
    }
}
